package com.moss.cloud.common.security.annotation.aspect;

import com.moss.cloud.common.core.constant.Constant;
import com.moss.cloud.common.core.exception.SystemErrorType;
import com.moss.cloud.common.core.utils.BooleanHandel;
import com.moss.cloud.common.core.utils.UserContextHolder;
import com.moss.cloud.common.security.annotation.RequiresTenant;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * 租户权限校验实现--某些接口或者方法必须是有租户的用户才可以操作
 *
 * @author 瑾年
 * @data 2020/05/05
 */
@Slf4j
@Aspect
@Component
public class RequiresTenantImpl {
    /**
     * 环绕通知
     *
     * @author 瑾年
     * @data 2020/05/05
     */
    @Around("@annotation(requiresTenant)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint, RequiresTenant requiresTenant) throws Throwable {
        Long tenantId = UserContextHolder.getTenantId();
        BooleanHandel.isTrue(requiresTenant != null).trueHandle(() -> {
            log.info("用户所属租户:{}",tenantId);
            BooleanHandel.trueThrow(Constant.NO_TENANT_AND_DEPT.equals(tenantId)).throwMessage(SystemErrorType.NO_TENANT);
        });
        return proceedingJoinPoint.proceed();
    }

}
